import re

from src.formats.ReadZipFiles import ReadZipFile
import os
import gzip


def getSrcPath():
    return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))


class ESRISlpkManager:
    def __init__(self, filePath):
        self.srcPath = getSrcPath()
        # 打包文件路径
        self.filePath = filePath
        # 打包文件zipFile对象,提供读取打包文件内文件的方法
        self.tilesFile = ReadZipFile(filePath)
        self.jsonPath = '3dSceneLayer.json.gz'
        self.rootJsonPath = 'nodes/root/3dNodeIndexDocument.json.gz'
        self.nodeJSONFileName = '3dNodeIndexDocument.json.gz'
        self.SublayersJSONFileName = '3dSceneLayer.json.gz'
        self.nodepagesPath = 'nodepages/'
        self.nodepagesFormat = '.json.gz'
        self.nodeSharedJsonFileName = 'sharedResource.json.gz'
        self.geometriesPath = 'nodes/'
        self.geometriesFormat = '.bin.gz'
        self.texturesPath = 'nodes/'
        self.texturesFormat1 = '.bin.dds.gz'
        self.texturesFormat2 = '.bin'
        self.texturesFormat3 = '.jpg'

    def getJson(self):
        # '3dSceneLayer.json.gz' 数据
        json = self.tilesFile.readSubFile(self.jsonPath)
        return json

    def getRootJson(self):
        json = self.tilesFile.readSubFile(self.rootJsonPath)
        return json

    def getNodeJson(self, tilename):
        # 'nodes/2247/3dNodeIndexDocument.json.gz'
        json = self.tilesFile.readSubFile('nodes/' + tilename + '/' + self.nodeJSONFileName)
        return json

    def getSublayersJson(self, tilename):
        # sublayers/8/3dSceneLayer.json.gz
        json = self.tilesFile.readSubFile('sublayers/' + tilename + '/' + self.SublayersJSONFileName)
        return json

    def getNodeSharedJson(self, tilename):
        json = self.tilesFile.readSubFile('nodes/' + tilename + '/shared/' + self.nodeSharedJsonFileName)
        return json

    def getNodepages(self, filename):
        json = self.tilesFile.readSubFile(self.nodepagesPath + filename + self.nodepagesFormat)
        return json

    def getGeometries(self, tilenum, filename):
        json = self.tilesFile.readSubFile(
            self.geometriesPath + tilenum + '/geometries/' + filename + self.geometriesFormat)
        return json

    def getGetTextures(self, tilenum, filename):
        json = ''
        try:
            try:
                json = self.tilesFile.readSubFile(
                    self.texturesPath + tilenum + '/textures/' + filename + self.texturesFormat1)
            except:
                json = self.tilesFile.readSubFile(
                    self.texturesPath + tilenum + '/textures/' + filename + self.texturesFormat2)
                json = gzip.compress(json)
        except:
            json = self.tilesFile.readSubFile(
                self.texturesPath + tilenum + '/textures/' + filename + self.texturesFormat3)
            json = gzip.compress(json)
        return json

    def getslpks_path(self, full_path):
        suffix = self.get_zip_suffix(full_path)
        json = self.tilesFile.readSubFile(full_path + suffix)
        return json

    @staticmethod
    def get_zip_suffix(full_path):
        path = full_path.split('/')
        path_num = len(path)
        suffix = '.json.gz'
        if re.match('root', path[path_num - 1]):
            suffix = '/3dNodeIndexDocument.json.gz'
        if re.match('attributes', path[path_num - 3]) and re.match('f_', path[path_num - 2]):
            suffix = '.bin.gz'
        if re.match('geometries', path[path_num - 2]):
            suffix = '.bin.gz'
        if re.match('share', path[path_num - 1]):
            suffix = '/sharedResource.json.gz'
        if re.match('nodes', path[path_num - 1]):
            suffix = '/3dNodeIndexDocument.json.gz'
        return suffix

    # 根据路径获取文件
    #   根据路径获取包内文件，如果报错那么
    #       1.判断其是否是文件夹，然后遍历拼合info文件中的默认文件名，首先对其进行存在性判断，若存在则返回
    #       2.如果是文件，遍历拼合info文件中的默认扩展名，首先对其进行存在性判断，若存在则返回
    def getFile(self, full_path, fileName, file_ext):
        suffix, mimetype, ziptype = self.get_suffix(full_path, fileName, file_ext)
        json = self.tilesFile.readSubFile(full_path + suffix)
        self.tilesFile.closeSubFile(full_path + suffix)
        return json, ziptype, mimetype

    def get_suffix(self, full_path, file_name, file_suffix):
        tag = self.tilesFile.pathIsExists(full_path + '/')
        suffix = ''
        ziptype = ''
        mimetype = 'text/html'
        if tag == 1:
            for filename in file_name:
                if self.tilesFile.pathIsExists(full_path + '/' + filename) == 1:
                    suffix = '/' + filename
        else:
            for suffix_name in file_suffix:
                if self.tilesFile.pathIsExists(full_path + suffix_name) == 1:
                    suffix = suffix_name
        if suffix.find('.json') > 0:
            mimetype = 'application/json'
        if suffix.find('.gz') > 0:
            ziptype = 'gzip'
        return suffix, mimetype, ziptype


if __name__ == '__main__':
    pass